package com.bykj.taketurns.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bykj.taketurns.R;
import com.bykj.taketurns.model.PersonalBean;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/18.
 * 本类用于展示用户界面参与的adapter
 */

public class PersonalRecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<PersonalBean.PartakeListBean> partakeList;
    private final int VIDEO_TYPE=0;
    private final int TEXT_TYPE=1;
    public PersonalRecyAdapter(Context context){
        this.mContext=context;
    }
    public void setData(List<PersonalBean.PartakeListBean> partakeList){
        this.partakeList=partakeList;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if(viewType==VIDEO_TYPE){
            view=View.inflate(mContext,R.layout.personal_recy_item,null);
            return new PersonalRecyViewHolder(view);
        }else {
            view=View.inflate(mContext,R.layout.person_text_item,null);
            return new PersonalTextViewHolder(view);
        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof PersonalRecyViewHolder){
            ((PersonalRecyViewHolder) holder).mIvPortraitItem.setText(partakeList.get(position).getStoryVideo().getPname());
            Glide.with(mContext).load(partakeList.get(position).getStoryVideo().getSvimg()).into(((PersonalRecyViewHolder) holder).mPersonalRecyImgConver);
            String time = getTime(partakeList.get(position).getStoryVideo().getSvaddtime());
            ((PersonalRecyViewHolder) holder).mPerRecyTvTime.setText(time);
            ((PersonalRecyViewHolder) holder).mPerRecyTvBrowerCon.setText(partakeList.get(position).getStoryVideo().getSvbrowse()+"");
            ((PersonalRecyViewHolder) holder).mPerRecyTvCommentCon.setText(partakeList.get(position).getCountPing()+"");
            ((PersonalRecyViewHolder) holder).mPerRecyTvLikedCon.setText(partakeList.get(position).getLikeCount()+"");
            ((PersonalRecyViewHolder) holder).mTvPersonalStage.setText(partakeList.get(position).getStoryVideo().getUnickname());
            ((PersonalRecyViewHolder) holder).mPerRecyTvStageName.setText(partakeList.get(position).getStoryVideo().getPname());
        }else if(holder instanceof PersonalTextViewHolder){
            ((PersonalTextViewHolder) holder).mIvPerTextItem.setText(partakeList.get(position).getStoryCharacter().getPname());
            ((PersonalTextViewHolder) holder).mPerTextRecyTvConver.setText(partakeList.get(position).getStoryCharacter().getSccontent());
            ((PersonalTextViewHolder) holder).mPerTextRecyTvCommentCon.setText(partakeList.get(position).getCountPing()+"");
            ((PersonalTextViewHolder) holder).mPerTextRecyTvLikedCon.setText(partakeList.get(position).getLikeCount()+"");
            ((PersonalTextViewHolder) holder).mPerTextRecyTvBrowerCon.setText(partakeList.get(position).getStoryCharacter().getScbrowse()+"");
            ((PersonalTextViewHolder) holder).mTvPerTextStage.setText(partakeList.get(position).getStoryCharacter().getPname());
            ((PersonalTextViewHolder) holder).mPerTextRecyTvStageName.setText(partakeList.get(position).getStoryCharacter().getUnickname());
        }

    }

    @Override
    public int getItemCount() {
        return partakeList.size();
    }
    //管理条目的viewholder
    public class PersonalRecyViewHolder extends RecyclerView.ViewHolder{

        public TextView mIvPortraitItem;
        public TextView mTvPersonalStage;
        public TextView mTvPersonalLast;
        public TextView mTvPersonalTopic;
        public RelativeLayout mRelaPersonalItem;
        public ImageView mPersonalRecyImgConver;
        public TextView mPerRecyTvStageName;
        public TextView mPerRecyTvName;
        public TextView mPerRecyTvBrowerCon;
        public TextView mPerRecyTvLikedCon;
        public TextView mPerRecyTvCommentCon;
        public TextView mPerRecyTvTime;

        public PersonalRecyViewHolder(View itemView) {
            super(itemView);
            mIvPortraitItem= itemView.findViewById(R.id.iv_portrait_item);
            mTvPersonalStage = itemView.findViewById(R.id.tv_personal_stage);
            mTvPersonalLast = itemView.findViewById(R.id.tv_personal_last);
            mTvPersonalTopic = itemView.findViewById(R.id.tv_personal_topic);
            mRelaPersonalItem = itemView.findViewById(R.id.rela_personal_item);
            mPersonalRecyImgConver = itemView.findViewById(R.id.personal_recy_img_conver);
            mPerRecyTvStageName = itemView.findViewById(R.id.per_recy_tv_stage_name);
            mPerRecyTvName = itemView.findViewById(R.id.per_recy_tv_name);
            mPerRecyTvBrowerCon = itemView.findViewById(R.id.per_recy_tv_brower_con);
            mPerRecyTvLikedCon = itemView.findViewById(R.id.per_recy_tv_liked_con);
            mPerRecyTvCommentCon = itemView.findViewById(R.id.per_recy_tv_comment_con);
            mPerRecyTvTime = itemView.findViewById(R.id.per_recy_tv_time);
        }
    }
    public class PersonalTextViewHolder extends RecyclerView.ViewHolder{

        public final TextView mIvPerTextItem;
        public final TextView mTvPerTextStage;
        public final TextView mTvPerTextTopic;
        public final RelativeLayout mRelaPerTextItem;
        public final TextView mTvPerTextNum;
        public final TextView mPerTextRecyTvStageName;
        public final TextView mPerTextRecyTvName;
        public final TextView mPerTextRecyTvBrowerCon;
        public final TextView mPerTextRecyTvLikedCon;
        public final TextView mPerTextRecyTvCommentCon;
        public final TextView mPerTextRecyTvConver;

        public PersonalTextViewHolder(View itemView) {
            super(itemView);
            mIvPerTextItem = itemView.findViewById(R.id.iv_per_text_item);
            mTvPerTextStage = itemView.findViewById(R.id.tv_per_text_stage);
            mTvPerTextTopic = itemView.findViewById(R.id.tv_per_text_topic);
            mRelaPerTextItem = itemView.findViewById(R.id.rela_per_text_item);
            mTvPerTextNum = itemView.findViewById(R.id.tv_per_text_num);
            mPerTextRecyTvStageName = itemView.findViewById(R.id.per_text_recy_tv_stage_name);
            mPerTextRecyTvName = itemView.findViewById(R.id.per_text_recy_tv_name);
            mPerTextRecyTvBrowerCon = itemView.findViewById(R.id.per_text_recy_tv_brower_con);
            mPerTextRecyTvLikedCon = itemView.findViewById(R.id.per_text_recy_tv_liked_con);
            mPerTextRecyTvCommentCon = itemView.findViewById(R.id.per_text_recy_tv_comment_con);
            mPerTextRecyTvConver = itemView.findViewById(R.id.per_text_recy_tv_conver);
        }
    }

    @Override
    public int getItemViewType(int position) {
        PersonalBean.PartakeListBean bean = partakeList.get(position);
        if(bean.getStoryVideo()!=null){
            return VIDEO_TYPE;
        }else {

            return TEXT_TYPE;
        }

    }
    //获取时间差
    public String getTime(String time){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeNum=null;

        try
        {
            Date d1 = df.parse(time);
            Date d2 =  new Date(System.currentTimeMillis());
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别


            long days = diff / (1000 * 60 * 60 * 24);
//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
            timeNum=days+"天前";


        }
        catch (Exception e)
        {
        }
        return timeNum;
    }
}
